package cn.javadog.algorithm.e601_1000;

import java.util.LinkedList;


/**
 * 二叉树中第二小的节点
 * https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/
 * @author Sprite
 */
public class Algorithm671 {

	public static void main(String[] args) {
		System.out.println(Math.min(Integer.MAX_VALUE,Integer.MAX_VALUE));
	}

	public static int findSecondMinimumValue(TreeNode root) {
		if (root == null) return -1;

		LinkedList<TreeNode> queue = new LinkedList<>();
		queue.add(root);

		int min = Integer.MAX_VALUE;
		int base = root.val;
		boolean change = false;

		while(!queue.isEmpty()) {
			int len = queue.size();
			for(int i=0; i<len; i++) {
				TreeNode cur = queue.poll();
				if (cur.val>base){
					change = true;
					min = Math.min(cur.val,min);
				}else {
					if(cur.left!=null) queue.add(cur.left);
					if(cur.right!=null) queue.add(cur.right);
				}
			}
		}
		return change ? -1 : min;
	}

	protected static class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode(int x) { val = x; }
	}
}

